//
//  FirstViewController.swift
//  HSMainViewControllerHandler
//
//  Created by haisheng huang on 15/12/14.
//  Copyright © 2015年 haisheng huang. All rights reserved.
//

import UIKit
import LocalAuthentication



class FirstViewController: UIViewController {

    
    @IBOutlet weak var infiniteView: HSScrollView!
    @IBOutlet weak var label: UILabel!
    var waveView: WaveView?
    var progress: HSGradientProgress?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
//        let pageControl: UIPageControl = UIPageControl()
//        pageControl.backgroundColor = UIColor.redColor()
//        //pageControl.frame = CGRectMake(200, 200, 100, 37)
//        pageControl.numberOfPages = 5
//        pageControl.currentPage = 1
//        let size = pageControl.sizeForNumberOfPages(5)
//        var frame = pageControl.frame
//        frame.size = size
//        frame.origin = CGPointMake(200, 150)
//        pageControl.frame = frame
//        self.view.addSubview(pageControl)
        
        
        
//        self.view.addSubview(pageControl)
        
        
        self.infiniteView.layer.borderColor = UIColor.black.cgColor
        self.infiniteView.layer.borderWidth = 0.5
        
        let view1: UIView = UIView()
        view1.backgroundColor = UIColor.blue
        let view2: UIView = UIView()
        view2.backgroundColor = UIColor.green
        let view3: UIView = UIView()
        view3.backgroundColor = UIColor.red
        
        
        //self.infiniteView.loadAutoInfiniteScroll(2.5, views: [view1, view2, view3])
        
//        let aiView: HSInfiniteScrollView = HSInfiniteScrollView(frame: CGRect(x: 25, y: 300, width: UIScreen.main.bounds.width - 50, height: 100), data: [UIColor.blue, UIColor.green, UIColor.red], autoInterval: 2.0, type:HSPageControlType.none)
//        aiView.layer.borderColor = UIColor.purple.cgColor
//        aiView.layer.borderWidth = 1.5
//        aiView.setPageControlIndicator(UIColor.yellow, tintColor: UIColor.gray)
//        self.view.addSubview(aiView);
        
//        print(self.label.frame)
//        print(self.label.frame.maxY)
        
        self.waveView = WaveView.addToView(self.view, frame: CGRect.init(x: 0, y: 400.0, width: self.view.frame.width, height: 6.0))
        self.waveView?.waveColor = UIColor.init(red: 164.0/255.0, green: 174.0/255.0, blue: 240.0/255.0, alpha: 1.0)
        self.waveView?.waveTime = 0.0
        
        progress = HSGradientProgress.create(at: self.view, frame: CGRect.init(x: 10, y: 450.0, width: self.view.frame.width - 20.0, height: 10.0), deepColor: UIColor.red, lightColor: UIColor.yellow, backgroundColor: UIColor.init(white: 0.9, alpha: 1.0), value: 0.9, isCornerRadius: true, direction: GradientChangeDirection.right, isAnimated: true, duration: 3.0)
        //progress.value = 0.5
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    
    class func initStoryboardViewController() -> FirstViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        self.startUseTouchId()
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.waveView?.wave()
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.waveView?.stop()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
//        let pageControl: HSPageControl = HSPageControl.init(type: .middle, superFrame: self.view.superview!.frame, numberOfPages: 5, currentPage: 0)
//        pageControl.backgroundColor = UIColor.redColor()
//        self.view.addSubview(pageControl)
    }
 
    
    func startUseTouchId() -> Void {
        
        if #available(iOS 8.0, *) {
            
            let authenticationContext = LAContext()
            var error: NSError? = nil
            
            let isTouchIdAvailable = authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
            
            if isTouchIdAvailable {
                
                NSLog("恭喜，Touch ID可以使用！")
                
                //设置验证TouchID时弹出Alert的输入密码按钮的标题, 为""则没有输入密码按钮
                authenticationContext.localizedFallbackTitle = "输入密码"
                
//                if #available(iOS 9.0, *) {
//                    //这个属性应该是类似于支付宝的指纹开启应用，如果你打开他解锁之后，按Home键返回桌面，再次进入支付宝是不需要录入指纹的。因为这个属性可以设置一个时间间隔，在时间间隔内是不需要再次录入。默认是0秒，最长可以设置5分钟。
//                    authenticationContext.touchIDAuthenticationAllowableReuseDuration = 5.0
//                }
                
                authenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "需要验证您的指纹来确认您的身份信息", reply: { (success, evaluateError) in
                    
                    if success {
                        NSLog("恭喜，您通过了Touch ID指纹验证！")
                    } else {
                        NSLog("抱歉，您未能通过Touch ID指纹验证！\n\(evaluateError)")
                        
                        if evaluateError?._code == -2 {
                            print("Canceled by user")
                        } else if evaluateError?._code == -3 {
                            //其他验证渠道
                            print("Fallback authentication mechanism selected")
                        }
                    }
                })
                
                
            } else {
                NSLog("抱歉，Touch ID不可以使用！\n\(error)")
                if error?.code == -5 {
                    print("Passcode not set")
                }
            }
            
            
            
        }
        
        
    }
    
    
    
}
